package com.shop.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.admin.constant.PostToExamineConstant;
import com.shop.admin.dao.PostCommentDao;
import com.shop.admin.dao.PostDao;
import com.shop.admin.entity.Post;
import com.shop.admin.entity.PostComment;
import com.shop.admin.entity.ShopOrder;
import com.shop.admin.service.PostCommentService;
import com.shop.admin.service.PostService;
import com.shop.admin.utils.PageUtils;
import com.shop.admin.vo.AdminErrandVo;
import com.shop.admin.vo.PostCommentVo;
import com.shop.admin.vo.PostPageVo;
import com.shop.admin.vo.PostToExamineVo;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.ws.rs.POST;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PostServiceImpl extends ServiceImpl<PostDao, Post> implements PostService {

    @Resource
    private PostDao postDao;

    @Resource
    private PostCommentDao postCommentDao;

    @Resource
    private PostCommentService postCommentService;

    @Override
    public PageUtils postList(Map<String, Object> params) {
        Page<PostPageVo> page = new Page<>(Long.parseLong(params.get("page").toString()),Long.valueOf(params.get("limit").toString()) );
        Page<PostPageVo> resPage = postDao.queryPostPageVoList(page,params);
        List<PostPageVo> records = resPage.getRecords();
        List<PostPageVo> postPageVos = records.parallelStream().map(p -> {
            PostPageVo vo = new PostPageVo();
            BeanUtils.copyProperties(p, vo);
            int count = postCommentService.count(new LambdaQueryWrapper<PostComment>().eq(PostComment::getPostId, p.getId()));
            vo.setPostCommentCount(count);
            return vo;
        }).collect(Collectors.toList());
        resPage.setRecords(postPageVos);
        PageUtils pageUtils = new PageUtils(resPage);
        return pageUtils;
    }

    @Override
    public PageUtils commentList(Map<String, Object> params) {
        Page<PostCommentVo> page = new Page<>(Long.parseLong(params.get("page").toString()),Long.valueOf(params.get("limit").toString()) );
        Page<PostCommentVo> resPage = postCommentDao.queryPostCommentVoList(page,params);
        List<PostCommentVo> postCommentList = resPage.getRecords();
        List<Integer> fatherIds = postCommentList.stream()
                .filter(postComment -> postComment.getFatherId() != null)
                .map(PostCommentVo::getFatherId)
                .collect(Collectors.toList());
        //根据父级评论的id查询对应的message，并设置到子评论对象中
        Map<Integer, String> fatherMessageMap = new HashMap<>();
        if (fatherIds.size() > 0) {
            // 查询father_id对应的message并存储到Map中
            List<PostComment> fatherComments = postCommentService.list(new QueryWrapper<PostComment>().lambda()
                    .select(PostComment::getId,PostComment::getMessage)
                    .in(PostComment::getId, fatherIds)
            );
            fatherComments.forEach(comment -> fatherMessageMap.put(comment.getId(), comment.getMessage()));
        }
        postCommentList.stream()
                .filter(postComment -> postComment.getFatherId() != null && fatherMessageMap.containsKey(postComment.getFatherId()))
                .forEach(postComment -> postComment.setFatherMessage(fatherMessageMap.get(postComment.getFatherId())));
        PageUtils pageUtils = new PageUtils(resPage);
        return pageUtils;
    }

    @Override
    public void toExamine(PostToExamineVo vo) {
        Post post = new Post();
        post.setId(vo.getPostId());
        if (vo.getStatus().equals(PostToExamineConstant.StatusEnum.RELEASED_AFTER_REVIEW.getCode())){
            post.setCheckStatus(PostToExamineConstant.StatusEnum.RELEASED_AFTER_REVIEW.getCode());
            this.updateById(post);
        }
        if (vo.getStatus().equals(PostToExamineConstant.StatusEnum.POST_REVIEW_REJECTED.getCode())){
            post.setCheckStatus(PostToExamineConstant.StatusEnum.POST_REVIEW_REJECTED.getCode());
            this.updateById(post);
        }
//        Page<PostPageVo> page = new Page<>(vo.getPage(), vo.getLimit());
//        Page<PostPageVo> resPage = postDao.queryPostPageVoList(page,null);
//        PageUtils pageUtils = new PageUtils(resPage);
    }

    @Override
    public PageUtils postToExamineList(Map<String, Object> params) {
        Page<PostPageVo> page = new Page<>(Long.parseLong(params.get("page").toString()),Long.valueOf(params.get("limit").toString()) );
        Page<PostPageVo> resPage = postDao.queryPostToExamineList(page,params);
        PageUtils pageUtils = new PageUtils(resPage);
        return pageUtils;
    }

    @Override
    @SneakyThrows
    public void deletePost(List<Long> asList) {
        for (Long aLong : asList) {
            Post post = new Post();
            post.setId(aLong.intValue());
            post.setIsDelete(0);
            this.updateById(post);
            PostComment postComment = new PostComment();
            postComment.setIsDeleted(0);
            postCommentService.update(postComment,new LambdaQueryWrapper<PostComment>().eq(PostComment::getPostId,aLong.intValue()));
        }
    }

    @Override
    public void deletePostComment(List<Long> asList) {
        for (Long aLong : asList) {
            PostComment postComment = new PostComment();
            postComment.setId(aLong.intValue());
            postComment.setIsDeleted(0);
            postCommentService.updateById(postComment);
        }
    }

    @Override
    public PageUtils postTopList(Map<String, Object> params) {
        Long pageValue = params.get("page") == null ? 1L : Long.parseLong(params.get("page").toString());
        Long limitValue = params.get("limit") == null ? 0L : Long.parseLong(params.get("limit").toString());
        Page<PostPageVo> page = new Page<>(pageValue, limitValue);
        if (limitValue == 0) {
            List<PostPageVo> resPage = postDao.queryPostTopVoList(params);
            List<PostPageVo> postTopPageVoList = resPage.parallelStream().map(p -> {
                PostPageVo vo = new PostPageVo();
                BeanUtils.copyProperties(p, vo);
                int count = postCommentService.count(new LambdaQueryWrapper<PostComment>().eq(PostComment::getPostId, p.getId()));
                vo.setPostCommentCount(count);
                return vo;
            }).collect(Collectors.toList());
            return new PageUtils(postTopPageVoList,postTopPageVoList.size());
        }
        Page<PostPageVo> resPage = postDao.queryPostTopPageVoList(page,params);
        List<PostPageVo> postPageVos = resPage.getRecords().parallelStream().map(p -> {
            PostPageVo vo = new PostPageVo();
            BeanUtils.copyProperties(p, vo);
            int count = postCommentService.count(new LambdaQueryWrapper<PostComment>().eq(PostComment::getPostId, p.getId()));
            vo.setPostCommentCount(count);
            return vo;
        }).collect(Collectors.toList());
        resPage.setRecords(postPageVos);
        return new PageUtils(resPage);
    }

    @Override
    public void topPostSave(List<Long> asList) {
        for (int i = 0; i < asList.size() ; i++) {
            Post post = new Post();
            post.setId(asList.get(i).intValue());
            post.setIsTop(i+1);
            this.updateById(post);
        }
    }

    @Override
    public void topPostDelete(List<Long> asList) {
        for (int i = 0; i < asList.size(); i++) {
            postDao.updatePostToNull(asList.get(i).intValue());
        }
    }
}
